package lib.bone.admin.controller.nepu.fix;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lib.bone.access.constant.ResearchTypeConst;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.flow.domain.entity.FlowWork;
import lib.bone.flow.service.FlowWorkService;
import lib.bone.nepudata.domain.entity.NepuTopic;
import lib.bone.nepudata.service.impl.NepuTopicServiceImpl;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("nepu/workloads/topic/fix")
@Slf4j
@Api(tags = "纵向课题问题修复")
@AllArgsConstructor
public class TopicFixController {
    NepuTopicServiceImpl topicService;

    FlowWorkService flowWorkService;

    @PostMapping("topic")
    @ApiOperation(value = "清理不存在流程信息的实例")
    public ResponseDTO<Object> cleanNoFlowTopic(@RequestParam("topicId") String topicId){
        topicService.deleteById(topicId);
        return ResponseDTO.succMsg("删除成功");
    }

    @PostMapping("topic/all")
    @ApiOperation(value = "清理全部不存在流程信息的实例")
    public ResponseDTO<Object> cleanAllNoFlowTopic(){
        FlowWork flowWork = new FlowWork();
        flowWork.setServiceType(ResearchTypeConst.TOPIC);
        Example<FlowWork> example = Example.of(flowWork);
        List<FlowWork> flows = flowWorkService.getAllFlowWork(example);
        List<NepuTopic> topics = topicService.getAll();
        List<NepuTopic> noFlowTopics = new ArrayList<>();
        Set<Long> userIds = new HashSet<>();
        for (NepuTopic item : topics){
            boolean hasFlow = false;
            for (FlowWork flow:flows){
                if (flow.getServiceId().equals(item.getId())){
                    hasFlow = true;
                }
            }
            if (!hasFlow){
                noFlowTopics.add(item);
                userIds.add(item.getLeaderId());
            }
        }

        List<String> topicIds = noFlowTopics.stream().map(NepuTopic::getId).collect(Collectors.toList());

        topicService.batchDelete(topicIds);
        return ResponseDTO.succMsg("删除成功");
    }

    @GetMapping("topic")
    @ApiOperation(value = "不存在流程信息的实例统计")
    public ResponseDTO<Object> countNoFlowTopic(@RequestParam("userId") String useId){
        FlowWork flowWork = new FlowWork();
        flowWork.setServiceType(ResearchTypeConst.TOPIC);
        flowWork.setInitiator(useId);
        Example<FlowWork> example = Example.of(flowWork);
        List<FlowWork> flows = flowWorkService.getAllFlowWork(example);
        // 获取本人全部topic
        NepuTopic topic = new NepuTopic();
        topic.setLeaderId(Long.parseLong(useId));
        //
        Example<NepuTopic> topicExample = Example.of(topic);
        List<NepuTopic> topics = topicService.getAll(topicExample);
        List<NepuTopic> noFlowTopics = new ArrayList<>();
        for (NepuTopic item : topics){
            boolean hasFlow = false;
            for (FlowWork flow:flows){
                if (flow.getServiceId().equals(item.getId())){
                    hasFlow = true;
                }
            }
            if (!hasFlow){
                noFlowTopics.add(item);
            }
        }
        return ResponseDTO.succData(
                noFlowTopics
        );
    }

    @GetMapping("topic/all")
    @ApiOperation(value = "不存在流程信息的实例统计")
    public ResponseDTO<Object> countNoFlowTopicAll(){
        FlowWork flowWork = new FlowWork();
        flowWork.setServiceType(ResearchTypeConst.TOPIC);
        Example<FlowWork> example = Example.of(flowWork);
        List<FlowWork> flows = flowWorkService.getAllFlowWork(example);
        List<NepuTopic> topics = topicService.getAll();
        List<NepuTopic> noFlowTopics = new ArrayList<>();
        Set<Long> userIds = new HashSet<>();
        for (NepuTopic item : topics){
            boolean hasFlow = false;
            for (FlowWork flow:flows){
                if (flow.getServiceId().equals(item.getId())){
                    hasFlow = true;
                }
            }
            if (!hasFlow){
                noFlowTopics.add(item);
                userIds.add(item.getLeaderId());
            }
        }


        return ResponseDTO.succData(
                noFlowTopics.size()
               // userIds
        );
    }
}
